package de.schnitzeljaeger.controller;

import de.schnitzeljaeger.R;
import de.schnitzeljaeger.model.dao.IGenericDao;
import de.schnitzeljaeger.model.dao.impl.DaoFactory;
import de.schnitzeljaeger.model.entity.IPaperChaseMasterData;
import de.schnitzeljaeger.model.entity.PaperChase;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

public class PaperChaseEdit extends Activity {
	
	static public final int CREATE = 0;
	static public final int UPDATE = 1;
	static public final String ID = "id";
	static private final int CREATE_WAYPOINT = Menu.FIRST;
	
	
	private Long id;
	private EditText txtName;
    private EditText txtDescription;
    private CheckBox chbxPublished;
    private LinearLayout waypointContainer;
    private IGenericDao<PaperChase> dao;
    
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.paper_chase_edit);
        setTitle(R.string.schnitzeljagd_edit);
        
        dao = DaoFactory.create(PaperChase.class, getApplicationContext());
        txtName = (EditText) findViewById(R.id.name);
        txtDescription = (EditText) findViewById(R.id.description);
        chbxPublished = (CheckBox) findViewById(R.id.published);
        waypointContainer = (LinearLayout) findViewById(R.id.waypoints);
        Button btnSave = (Button) findViewById(R.id.save);

        id = savedInstanceState == null ? null :
        	 (Long) savedInstanceState.getSerializable(ID);
        
        if (id == null) {
            Bundle extras = getIntent().getExtras();
            id = extras != null ? extras.getLong(ID) :
            						null;
        }	

        populateFields();

        btnSave.setOnClickListener(new View.OnClickListener() {
        	@Override
        	public void onClick(View view) {
        		setResult(RESULT_OK);
        		finish();
        	}
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, CREATE_WAYPOINT, 0, R.string.create_waypoint);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case CREATE_WAYPOINT:
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        save();
        outState.putSerializable(ID, id);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        save();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }
    
    private void populateFields() {
        if (id != null) {
        	waypointContainer.setVisibility(View.VISIBLE);
        	IPaperChaseMasterData entity = dao.find(IPaperChaseMasterData.class, id);
            
            txtName.setText(entity.getName());
            txtDescription.setText(entity.getDescription());
            chbxPublished.setChecked(entity.isPublished());
        } else
        	waypointContainer.setVisibility(View.INVISIBLE);
    }

    private void save() {
    	PaperChase entity = new PaperChase();
    	IPaperChaseMasterData specific = entity;
    	
        specific.setName(txtName.getText().toString());
        specific.setDescription(txtDescription.getText().toString());
        specific.setPublished(chbxPublished.isChecked());

        if (id == null)
            id = dao.create(entity);
        else
            dao.update(IPaperChaseMasterData.class, specific, id);
    }
}
